package org.stvd.service.common.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.common.utils.ServiceResult;
import org.stvd.core.util.DateUtil;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.common.AuditDetail;
import org.stvd.entities.common.AuditFlow;
import org.stvd.repository.common.AuditDetailDao;
import org.stvd.repository.common.AuditFlowDao;
import org.stvd.service.base.impl.BaseServiceImpl;
import org.stvd.service.common.AuditFlowService;

/**
 * 审核记录Service实现类
 * @author houzx
 */
@Service("auditFlowService")
public class AuditFlowServiceImpl extends BaseServiceImpl<AuditFlow> implements AuditFlowService{

    @Resource(name ="AuditFlowDao")
    private AuditFlowDao auditFlowDao;
    @Resource(name ="AuditDetailDao")
    private AuditDetailDao auditDetailDao;
    
    @Override
    public AuditFlow getAuditFlowByInfoId(String sourceTable, String sourceId) {
    	return auditFlowDao.getAuditFlowByInfoId(sourceTable, sourceId);
    }
    
    @Override
    public Map<String, Object> getNewAuditFlowDetail(String sourceTable, String sourceId) {
    	List<Map<String, Object>> list =  auditFlowDao.listAllAuditFlowDetail(sourceTable, sourceId);
    	if(list!=null && list.size() > 0) {
    		return list.get(0);
    	}
    	return null;
    }
    
    @Override
    public List<Map<String, Object>> listAllAuditFlowDetail(String sourceTable, String sourceId) {
    	return auditFlowDao.listAllAuditFlowDetail(sourceTable, sourceId);
    }
    
    @Override
    public List<Map<String, Object>> listUserAuditFlowDetail(String sourceTable, String sourceId, String auditorId) {
    	return auditFlowDao.listUserAuditFlowDetail(sourceTable, sourceId, auditorId);
    }
    
    @Override
    @Transactional
    public ServiceResult<Map<String, Object>> insertNewAuditFlow(String sourceTable, String sourceId, String auditorId,
        String auditorName, String auditStatus, String auditContent) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<>();
        if(StringUtil.isEmpty(sourceTable) || StringUtil.isEmpty(sourceId) 
            || StringUtil.isEmpty(auditorId) || StringUtil.isEmpty(auditStatus)
            || StringUtil.isEmpty(auditContent)) {
            serviceResult.setParamError("参数获取错误");
            return serviceResult;
        }
        //更新/新增审核记录
        AuditFlow auditFlow = auditFlowDao.getAuditFlowByInfoId(sourceTable, sourceId);
        String flowId = "";
        if(auditFlow == null) {
            flowId = StringUtil.getRandomUUID();
            auditFlow = new AuditFlow();
            auditFlow.setId(flowId);
            auditFlow.setSourceTable(sourceTable);
            auditFlow.setSourceId(sourceId);
            auditFlow.setCurrAuditor(auditorId);
            auditFlow.setFinalResult(auditStatus);
            auditFlow.setCreateTime(DateUtil.getSystemDate());
            auditFlow.setModifyTime(DateUtil.getSystemDate());
        }else {
            flowId = auditFlow.getId();
            auditFlow.setFinalResult(auditStatus);
            auditFlow.setModifyTime(DateUtil.getSystemDate());
        }
        // 新增审核记录详情
        AuditDetail auditDetail = new AuditDetail();
        auditDetail.setId(StringUtil.getRandomUUID());
        auditDetail.setFlowId(flowId);
        auditDetail.setAuditorId(auditorId);
        auditDetail.setAuditorName(auditorName);
        auditDetail.setStatus(auditStatus);
        auditDetail.setContent(auditContent);
        auditDetail.setCreateTime(DateUtil.getSystemDate());
        
        auditFlowDao.update(auditFlow);
        auditDetailDao.insert(auditDetail);
        
        serviceResult.setMessage("新增成功");
        return serviceResult;
    }
    
    @Override
    @Transactional
    public ServiceResult<Map<String, Object>> deleteAuditFlowBySource(String sourceTable, String sourceId) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<>();
        if(StringUtil.isEmpty(sourceTable) || StringUtil.isEmpty(sourceId)) {
            serviceResult.setParamError("参数获取失败");
            return serviceResult;
        }
        try {
            auditDetailDao.deleteAuditDetailBySource(sourceTable, sourceId);
            auditFlowDao.deleteAuditFlowBySource(sourceTable, sourceId);
            serviceResult.setMessage("删除成功");
        } catch (Exception e) {
            serviceResult.setDataError("删除失败，失败原因："+e.getMessage());
        }
        return serviceResult;
    }
    
    @Override
    @Transactional
    public ServiceResult<Map<String, Object>> deleteAuditFlowById(String flowId) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<>();
        if(StringUtil.isEmpty(flowId)) {
            serviceResult.setParamError("参数获取失败");
            return serviceResult;
        }
        try {
            auditDetailDao.deleteAuditDetailByFlowId(flowId);
            auditFlowDao.delete(AuditFlow.class, flowId);
            serviceResult.setMessage("删除成功");
        } catch (Exception e) {
            serviceResult.setDataError("删除失败，失败原因："+e.getMessage());
        }
        return serviceResult;
    }
}
